﻿"use strict";
define(['admin/app', "service/admin/orderService", "service/baseService"], function (app) {
    app.controller('orderController', function ($scope, $stateParams, $location, $uibModal, $state, utils, orderService, baseService) {
        $scope.query = {
            page: 1, size: 20, q: '', start_time: '', end_time: '', merchant_no: '', order_no: '', order_status: 0,order_type:''
        };
        $scope.update_type = false;
        $scope.refund_model = { order_id: 0, amount: 0, remark: '', status: 2, type: 1, type_value: '1' };
        angular.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                orderService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            get: function () {
                if ($stateParams.id > 0) {
                    orderService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '详情';
                }
            },
            goBack: function () {
                history.back();
            },
            get_order_status: function (status) {
                //:待付款1,待发货2,已完成3,已关闭4，待确认5
                if (status == 1)
                    return "待付款";
                else if (status == 2)
                    return "待发货";
                else if (status == 3)
                    return "待确认";
                else if (status == 4)
                    return "已完成";
                else if (status == 5)
                    return "已关闭";
                else
                    return "异常";
            },
            kdniao_change: function (select_item, bind_model) {
                $scope.kdniao_selected = bind_model;
            },
            update_shipping: function () {
                $scope.update_type = true;
            },
            save_shipping: function () {
                if ($scope.kdniao_selected.value == undefined) {
                    utils.notify('请选择快递物流', "warning");
                    return;
                }
                if ($scope.model.ship_order_number == undefined) {
                    utils.notify('请填写快递单号', "warning");
                    return;
                }
                $scope.psdisabled = true;
                let order = {
                    order_no: $scope.model.order_no,
                    express_no: $scope.kdniao_selected.value,
                    express_company_name: $scope.kdniao_selected.text,
                    ship_order_number: $scope.model.ship_order_number
                };
                orderService.shipping(order).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000) {
                        $scope.model.shipping.ship_order_number = $scope.model.ship_order_number;
                        $scope.model.shipping.express_companyname = $scope.kdniao_selected.text;
                        $scope.model.order_status = 3;
                    }
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                $location.search('page', $scope.query.page);
            },
            open_refund_dialog: function (model) {
                $uibModal.open({
                    templateUrl: 'open_refund_dialog.html',
                    controller: 'open_refund_dialog_ctrl',
                    resolve: {
                        refund_model: function () {
                            var refund_model = $scope.refund_model;
                            return refund_model;
                        },
                        model: function () {
                            return model;
                        }
                    }
                }).result.then(function (model) {
                    methods.get();
                });
            },
            confirmOrder: function (id) {
                var modal = utils.confirm({ msg: "是否确认收货该订单", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    orderService.confirmOrder(id).success(function () {
                        history.back();
                    });
                })
            },
        }

        angular.extend($scope, methods);
        if ($state.includes('app.order.detail')) {
            methods.get();
            baseService.dictionary().success(function (response) {
                $scope.kdniao = JSON.parse(response.data);
            });
        } else {
        	methods.search();
    	}
    });


    app.controller('open_refund_dialog_ctrl', function ($scope, $uibModalInstance, utils, orderService, model) {
        $scope.orderRefund = function () {
            $scope.psdisabled = true;
            $scope.refund_model.type = Number($scope.refund_model.type_value);
            $scope.refund_model.order_id = model.id;
            $scope.refund_model.status = 2;
            orderService.orderRefund($scope.refund_model).success(function (response) {
                utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                if (response.result_code == 1000)
                    $uibModalInstance.close(model);
                else
                    $scope.psdisabled = false;
            }).error(function () { $scope.psdisabled = false; });
        };

        $scope.cancel = function () {
            $uibModalInstance.dismiss('cancel');
        };
    });
});
